import { service, crudParamsSerializer } from '@/utils/request'
import { CreateQueryParams } from '@nestjsx/crud-request'
import { Album } from './album.dto'
import { CrudPaginatedRes } from '..'

export const defaultAlbumData: Partial<Album> = {
  title: '',
  description: '',
  coverImage: '',
  recordLabel: '',
  types: [],
  visible: false,
  favCount: 0,
  belongsToArtists: [],
  hasProducers: []
}

export function getAlbums (params: CreateQueryParams) {
  return service.get<Album[] | CrudPaginatedRes<Album>>(
    '/resources/albums',
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function getAlbumById (id: number | string, params: CreateQueryParams) {
  return service.get<Album>(
    `/resources/albums/${id}`,
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function checkFavAlbum (id: number | string) {
  return service.request({
    url: `/resources/albums/${id}/fav`,
    method: 'get'
  })
}

export function setFavAlbum (id: number | string) {
  return service.request({
    url: `/resources/albums/${id}/fav`,
    method: 'post'
  })
}

export function removeFavAlbum (id: number | string) {
  return service.request({
    url: `/resources/albums/${id}/fav`,
    method: 'delete'
  })
}

export function createAlbum (data: Partial<Album>, params?: CreateQueryParams) {
  return service.post<Album>(
    '/resources/albums',
    data,
    {
      params,
      paramsSerializer: crudParamsSerializer
    }
  )
}

export function updateAlbum (id: number | string, data: Partial<Album>, params?: CreateQueryParams) {
  return service.patch<Album>(
     `/resources/albums/${id}`,
     data, {
       params,
       paramsSerializer: crudParamsSerializer
     }
  )
}
